package com.tanhua.server.controller;

import com.tanhua.server.service.UserService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author : xyiaao
 * @createDate : 2022/3/14 11:07
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;


    /**
     * 获取登录验证码
     *
     * @param map
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody LoginInfo map) {

        String phone = map.getPhone();
        userService.sendMsg(phone);
        // return ResponseEntity.status(500).body("出错啦！");
        return ResponseEntity.ok(null);
    }

    /**
     * token : eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE2NDc5NDcxODksIm1vYmlsZSI6IjEzODAwMTM4MDAwIiwiaWQiOjEwNn0.EVKMDKfZnQrfVaxpcqITJlzZ-2qwFM4mkziiLCc3wJLzIKNhUaYQXFX2gdKpwRbpsYuCbwrc0TncKKsi_xkRIA
     *
     * @param map
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody LoginInfo map) {
        // 1、调用map集合获取请求参数
        String phone = map.getPhone();
        String code = map.getVerificationCode();
        // 2、调用userService完成用户登录
        Map retMap = userService.loginVerification(phone, code);
        // 3、构造返回
        return ResponseEntity.ok(retMap);
    }

    @Data
    private static class LoginInfo {

        private String phone;

        private String verificationCode;

    }


}
